package com.anhtuan.reminder.dialogs;

import com.anhtuan.reminder.R;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TimePicker;

public class TimePickerDialog extends DialogFragment {
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

		LayoutInflater inflater = (LayoutInflater) getActivity()
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		final View view = inflater.inflate(R.layout.timepicker, null, false);

		builder.setView(view)
				.setTitle(R.string.time_picker_title)
				.setPositiveButton(R.string.picker_button_ok_text,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								EditText editText = (EditText) getActivity()
										.findViewById(R.id.et_time);
								TimePicker timePicker = (TimePicker) view
										.findViewById(R.id.timePicker);

								String hour = "";
								String min = "";
								
								hour = timePicker.getCurrentHour() + "";
								min = timePicker.getCurrentMinute() + "";
								
								if (timePicker.getCurrentHour() < 10) {
									hour = "0" + hour;
								}
								
								if (timePicker.getCurrentMinute() < 10) {
									min = "0" + min;
								}
								
								String time = String.format(
										getResources().getString(
												R.string.time_format_string),
										hour,
										min);
								
								editText.setText(time);

								dismiss();
							}
						})
				.setNegativeButton(R.string.picker_button_cacel_text, null);

		return builder.create();
	}
}
